package com.handpay.nocardpay.baselibrary.account.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;

import com.handpay.nocardpay.baselibrary.NoCardPayApplication;
import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.account.view.IShowHideFragmentListener;
import com.handpay.nocardpay.baselibrary.account.view.fragment.FastBillLoginFragment;
import com.handpay.nocardpay.baselibrary.account.view.fragment.GestureLoginFragment;
import com.handpay.nocardpay.baselibrary.account.view.fragment.NoCardLoginFragment;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.model.ActivityStack;
import com.handpay.nocardpay.baselibrary.commbase.view.activity.BaseActivity;
import com.handpay.nocardpay.baselibrary.config.NoCardPayApi;
import com.handpay.nocardpay.baselibrary.config.NoCardPayConfig;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.main.home.view.fragment.HomeFragment;
import com.handpay.nocardpay.baselibrary.main.mine.view.fragment.MineFragment;
import com.handpay.nocardpay.baselibrary.setting.view.fragment.GestureLockVerifyFragment;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;
import com.handpay.nocardpay.baselibrary.utils.NoCardPayConstant;
import com.handpay.nocardpay.baselibrary.utils.PreferenceManager;

import java.util.LinkedList;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by sxshi on 2017/8/23.
 */

public class LoginActivity extends BaseActivity implements IShowHideFragmentListener {
    private boolean isNoCardPay = true;//默认无卡支付登录
    private long TOUCH_TIME = 0;
    private SupportFragment[] mFragments = new SupportFragment[3];

    @Override
    public void initData() {
        if (getIntent() != null) {
            isNoCardPay = getIntent().getBooleanExtra(BaseProtocolKey.LOGIN_TYPE, true);
        }
        if (isNoCardPay) {
            String gesture_pwd = ClientEngine.getInstance().getRMSData(BaseProtocolKey.KEY_GESTURE_PWD);
            boolean bGestureSwitchStatus = PreferenceManager.getInstance(this).getBoolean(BaseProtocolKey.KEY_GESTURE_PWD_SWITCH_STATUS, true);//手势密码开关
            if (!TextUtils.isEmpty(gesture_pwd) && bGestureSwitchStatus) {
                showHideFragment(mFragments[NoCardPayConstant.GESTURE_LOGIN]);
            } else {
                showHideFragment(mFragments[NoCardPayConstant.NOCARDPAY_LOGIN]);
            }
        } else {
            showHideFragment(mFragments[NoCardPayConstant.FASTBILL_LOGIN]);
        }
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        NoCardLoginFragment noCardLoginFragment = findFragment(NoCardLoginFragment.class);
        FastBillLoginFragment fastBillLoginFragment = findFragment(FastBillLoginFragment.class);
        GestureLockVerifyFragment gestureLockVerifyFragment = findFragment(GestureLockVerifyFragment.class);
        if (noCardLoginFragment == null) {
            mFragments[NoCardPayConstant.NOCARDPAY_LOGIN] = NoCardLoginFragment.newInstance();
            mFragments[NoCardPayConstant.FASTBILL_LOGIN] = FastBillLoginFragment.newInstance();
            mFragments[NoCardPayConstant.GESTURE_LOGIN] = GestureLoginFragment.newInstance();
            loadMultipleRootFragment(R.id.fl_container, NoCardPayConstant.NOCARDPAY_LOGIN, mFragments[NoCardPayConstant.NOCARDPAY_LOGIN], mFragments[NoCardPayConstant.FASTBILL_LOGIN], mFragments[NoCardPayConstant.GESTURE_LOGIN]);
        } else {
            mFragments[NoCardPayConstant.NOCARDPAY_LOGIN] = noCardLoginFragment;
            mFragments[NoCardPayConstant.FASTBILL_LOGIN] = fastBillLoginFragment;
            mFragments[NoCardPayConstant.GESTURE_LOGIN] = gestureLockVerifyFragment;
        }

        ((NoCardLoginFragment) mFragments[NoCardPayConstant.NOCARDPAY_LOGIN]).setShowHideFragmentListener(this);
        ((FastBillLoginFragment) mFragments[NoCardPayConstant.FASTBILL_LOGIN]).setShowHideFragmentListener(this);
        ((GestureLoginFragment) mFragments[NoCardPayConstant.GESTURE_LOGIN]).setShowHideFragmentListener(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }


    @Override
    public void showFragment(int index) {
        showHideFragment(mFragments[index]);
    }

    @Override
    public void onBackPressedSupport() {
        if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
            finish();
            NoCardPayApplication.getApp().exitAppaction();
        } else {
            TOUCH_TIME = System.currentTimeMillis();
            DialogUtils.showToast(this, getString(R.string.press_again_exit));
        }
    }
}
